import request from '@/utils/request'

// 流程实例
export const getListProcess = data => {
    const queryString = objectToQueryStr(data);
    // console.log(queryString)
    return request({
        url: '/flow/monitor/listProcess',
        method: 'post',
        data: queryString,

    });
};

function objectToQueryStr(obj, filterNull = false) {
  let queryStr = "";
  Object.keys(obj).forEach(key => {
    if (filterNull && !obj[key]) {
      return;
    }
    queryStr += `&${key}=${obj[key] || ''}`
  });
  // console.log(queryStr);
  return queryStr.slice(1);
}

// 挂起/唤醒流程实例
export const enableProcess = (processInstanceId, enable) => {
    const url = (
        enable
        ? `/flow/monitor/run/${processInstanceId}`
        : `/flow/monitor/suspend/${processInstanceId}`
    );
    return request({
        url,
        method: 'get',
    });
}

// 执行实例
export const getListExecutions = data => {
    const queryString = data && objectToQueryStr(data);
    return request({
        url: '/flow/monitor/listExecutions',
        method: 'post',
        data: queryString,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
    });
}

// 运行历史
export const getListHistoryProcess = data => {
    const queryString = objectToQueryStr(data);
    return request({
        url: '/flow/monitor/listHistoryProcess',
        method: 'post',
        data: queryString,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
    });
}

// 获取某个历史或者变量
export const getListByTypeAndId = (type, processInstanceId) => {
    const url = type === "history" ? `/flow/monitor/history/${processInstanceId}` : `/flow/monitor/variables/${processInstanceId}`
    const queryString = objectToQueryStr({
        pageSize: 100,
        pageNum: 1,
        isAsc: "asc"
    });
    return request({
        url,
        method: 'post',
        data: queryString,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
    });
}

// 作业管理
export const getListJobs = (type, data) => {
    const queryString = objectToQueryStr(data);
    return request({
        url: `/flow/monitor/listJobs?type=${type}`,
        method: 'post',
        data: queryString,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
    });
}

export function openWindow(path) {
  const prefix = process.env.VUE_APP_TAB_URL_PREFIX;
  window.open(prefix + "" + path);
}

export function listToTree(list) {
  const nodeMap = {};
  const firstLevelList = list.filter(item => item.parentExecutionId === "0");
  firstLevelList.forEach(item => {
    const {executionId} = item;
    nodeMap[executionId] = item;
  });
  while (true) {
    list.forEach(item => {
      const {parentExecutionId, executionId} = item;
      if (nodeMap[executionId]) return;
      if (nodeMap[parentExecutionId]) {
        const parent = nodeMap[parentExecutionId];
        if (parent.children) {
          parent.children.push(item);
        } else {
          parent.children = [item];
        }
        nodeMap[executionId] = item;
      }
    });
    if (Object.keys(nodeMap).length === list.length) {
      return firstLevelList;
    }
  }
}

export function normalizeDateTimeString(rawDatetimeStr) {
  function genNumStr(num) {
    return Number(num) < 10 ? "0" + num : ("" + num)
  }
  if (rawDatetimeStr) {
    const dateObj = new Date(rawDatetimeStr);

    const yyyy = dateObj.getFullYear();
    const MM = dateObj.getMonth() + 1;
    const dd = dateObj.getDate();

    const HH = dateObj.getHours();
    const mm = dateObj.getMinutes();
    const ss = dateObj.getSeconds();

    return `${yyyy}-${genNumStr(MM)}-${genNumStr(dd)} ${genNumStr(HH)}:${genNumStr(mm)}:${genNumStr(ss)}`;
  }
}

